package servlets;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dataTypes.*;

@SuppressWarnings("serial")
public class businessRegister extends HttpServlet {

	public businessRegister() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {

			response.setContentType("text/html");

			String username = request.getParameter("username");
			//check availability 
			String password = request.getParameter("password");
			//check if legal
			String business = request.getParameter("business");
			String address = request.getParameter("address");
			int type = Integer.parseInt(request.getParameter("type"));
			String siteLink = request.getParameter("siteLink");
			String photoLink = request.getParameter("photoLink");
			String description = request.getParameter("description");

			business b = new business(username, sha1.SHA1(password.trim()).toLowerCase(), business, address, type, siteLink, photoLink, description);

			// String result = appManager.getInstance().businessReg(b);
			// use result for response
			
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				pm.makePersistent(b);
							}
			catch(Exception e){
		}
			finally {
				response.sendRedirect("/adminchose.html?bId="+b.getKey().getId());
				pm.close();
			}

			// send confirmation of registration

		} catch (Exception e) { 
			e.printStackTrace();
		}		

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}